/*
package com.lazy.utils.interceptor;


import com.lazy.utils.annotation.Idempotent;
import com.lazy.utils.common.FastJsonUtil;
import com.lazy.utils.exception.IdempotentCheckException;
import com.lazy.utils.lock.DistributedLockService;
import com.lazy.utils.utils.Result;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import redis.clients.jedis.JedisCluster;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.text.MessageFormat;
import java.util.concurrent.TimeUnit;

*/
/**
 * 接口幂等拦截器
 * @author zk
 * @since 2018-04-18 14:21
 *//*

@Aspect
@Component
public class IdempotentInterceptor {

    private Logger logger = LoggerFactory.getLogger(getClass());

    private static final String LAZY_REQ_IDEM_RESULT_KEY = "lazy:req:idem:result:{0}";

    private static final String LAZY_REQ_IDEM_COUNT_KEY = "lazy:req:idem:count:{0}";

    private static final String LAZY_REQ_IDEM_TRY_LOCK_KEY = "lazy:req:idem:try:lock:{0}";

    private static final String X_REQ_IDEM_ID = "X-REQ-IDEM-ID";

    @Autowired
    private JedisCluster jedisCluster;
    @Autowired
    private DistributedLockService distributedLockService;

    @Around("@annotation(com.lazy.utils.annotation.Idempotent)")
    public Result idempotentCheck(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Object[] args = proceedingJoinPoint.getArgs();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
        Method targetMethod = methodSignature.getMethod();
        Idempotent idempotent = targetMethod.getAnnotation(Idempotent.class);
        Result result;
        if (idempotent != null && idempotent.check()) {
            String xReqIdemId = request.getParameter(X_REQ_IDEM_ID);
            if (StringUtils.isEmpty(xReqIdemId)) {
                logger.error("需要做幂等的方法,但是未传入:{} ,未通过拦截器校验,拒绝调用", X_REQ_IDEM_ID);
                throw new IdempotentCheckException("E01003", X_REQ_IDEM_ID);
            } else {
                String reqIdemCountKey = MessageFormat.format(LAZY_REQ_IDEM_COUNT_KEY, xReqIdemId);
                String reqIdemTryLockKey = MessageFormat.format(LAZY_REQ_IDEM_TRY_LOCK_KEY, xReqIdemId);
                try {
                    if (distributedLockService.tryLock(reqIdemTryLockKey, 3, 30, TimeUnit.SECONDS)) {
                        int limitTime = getLimitTime(idempotent.timeOut(), idempotent.timeUnit());
                        String reqIdemResultKey = MessageFormat.format(LAZY_REQ_IDEM_RESULT_KEY, xReqIdemId);
                        long count = jedisCluster.incrBy(reqIdemCountKey, 1);
                        if (count == 1) {
                            result = (Result) proceedingJoinPoint.proceed(args);
                            if (result.getStatus() == 200) {
                                jedisCluster.expire(reqIdemCountKey, limitTime);
                                jedisCluster.set(reqIdemResultKey, FastJsonUtil.obj2json(result));
                                jedisCluster.expire(reqIdemResultKey, limitTime);
                            } else {
                                jedisCluster.del(reqIdemCountKey);
                            }
                        } else {
                            String data = jedisCluster.get(reqIdemResultKey);
                            if (StringUtils.isEmpty(data)) {
                                logger.error("幂等校验-当前请求 xReqIdemId :{} 距离第一次请求已经超过30分钟,预留数据已经失效了,请求时间超过30分钟属于违反业务规定,不予处理该请求", xReqIdemId);
                                throw new IdempotentCheckException("E01004", limitTime, reqIdemCountKey);
                            }
                            result = FastJsonUtil.json2obj(data, Result.class);
                            logger.info("幂等校验-成功拦截请求 xReqIdemId :{} 的请求,调用方法:{},返回結果result:{}", xReqIdemId, targetMethod.getName(), data);
                        }
                    } else {
                        logger.info("幂等校验-同一请求并发量过高,在3s内锁竞争未获取到锁, 幂等请求 xReqIdemId :{},调用方法:{}", xReqIdemId, targetMethod.getName());
                        throw new IdempotentCheckException("E01005", reqIdemCountKey);
                    }
                } catch (Exception e) {
                    jedisCluster.del(reqIdemCountKey);
                    if (e instanceof InterruptedException) {
                        logger.error("幂等校验-同一请求并发量过高,竞争锁发生异常,幂等请求 xReqIdemId :{},调用方法:{},异常信息 e:{}", xReqIdemId, targetMethod.getName(), e);
                        throw new IdempotentCheckException("E01006", reqIdemCountKey);
                    } else {
                        throw e;
                    }
                } finally {
                    distributedLockService.unlock(reqIdemTryLockKey);
                }
            }
        } else {
            result = (Result) proceedingJoinPoint.proceed(args);
        }
        return result;
    }

    */
/**
     * 将所有时间类型都转换为秒，如果是毫秒，
     * 不够转到1秒的就默认1800s
     *//*

    private int getLimitTime(int timeOut, TimeUnit timeUnit) {
        long limitTime = TimeUnit.SECONDS.convert(timeOut, timeUnit);
        if (limitTime == 0) {
            limitTime = 1800;
        }
        return (int) limitTime;
    }


}

*/
